package com.zhengdongzulin.zuke.app.main.apartment

import com.lxj.androidktx.core.*
import com.xuexiang.xui.widget.picker.widget.TimePickerView
import com.xuexiang.xui.widget.picker.widget.builder.TimePickerBuilder
import com.xuexiang.xui.widget.picker.widget.configure.TimePickerType
import com.zhengdongzulin.zuke.R
import com.zhengdongzulin.zuke.app.data.HouseTypeDetaisl
import com.zhengdongzulin.zuke.app.data.RoomDetails
import com.zhengdongzulin.zuke.base.*
import com.zhengdongzulin.zuke.databinding.ZkActivityReserveBinding
import com.zhengdongzulin.zuke.ext.*
import com.zhengdongzulin.zuke.utils.DateFormatConstants
import com.zhengdongzulin.zuke.utils.DateUtils
import java.text.SimpleDateFormat
import java.util.*


/**
 * @类名称: CLASS
 * @类描述: 预约
 * @创建人：Jeff
 * @创建时间：2021/6/9 20:59
 * @备注：
 */
class ZKReserveActivity : BaseActivity<ReserveVm, ZkActivityReserveBinding>() {
    var mTimePicker: TimePickerView? = null
    lateinit var mHouseTypeData: HouseTypeDetaisl//
    lateinit var mRoomDetaileData: RoomDetails//
    override fun getLayoutResId(): Int = R.layout.zk_activity_reserve

    override fun initVM(): ReserveVm = getVM(ReserveVm::class.java)

    override fun initView() {
        val calendar: Calendar = Calendar.getInstance()
        calendar.setTime(DateUtils.millis2Date(System.currentTimeMillis()))
        mTimePicker = TimePickerBuilder(
            this
        ) { date, v ->
            mView.time.text =
                DateUtils.date2String(date, SimpleDateFormat(DateFormatConstants.yyyyMMddHHmm))
        }
            .setTimeSelectChangeListener { }
            .setType(TimePickerType.DATE)
            .setCancelColor(color(R.color.color_1273f7))
            .setSubmitColor(color(R.color.color_1273f7))
            .setTitleColor(color(R.color.color_1273f7))
            .setDividerColor(color(R.color.color_1273f7))
            .setTextColorCenter(color(R.color.color_1273f7))
            .setTitleText("时间选择")
            .setOutSideCancelable(false)
            .setDate(calendar)
            .build()
    }

    override fun initData() {

        val userInfo = mShareViewModel.userInfo.value
        mView.name.setText(userInfo?.nameCn)
        mView.phone.setText(userInfo?.phone)


        if (intent.getStringExtra("type") == "houseDetails") {
            mHouseTypeData = mShareViewModel.houseDetails.value!!
            if (mHouseTypeData.picList != null && mHouseTypeData.picList.size > 0) {
                mView.ivTitle.loadImage(mHouseTypeData.picList[0].url)
            }
            mView.titleName.text = mHouseTypeData.roomTypeName
            mView.money.text = "￥${mHouseTypeData.houseMinMoney}"
            mView.money.appendSizeSpan("/月", 0.46f)
                .colorSpan(
                    "",
                    mView.money.text.length - 2..mView.money.text.length,
                    color(R.color.black_66)
                )
                .styleSpan("", 0..mView.money.text.length - 2)
            mView.desc.text =
                "${mHouseTypeData.doorName}\u0020\u0020${mHouseTypeData.area}㎡\u0020\u0020${mHouseTypeData.orientation}"
            mView.addressDesc.text = mHouseTypeData.address
        } else {
            mRoomDetaileData = mShareViewModel.roomDetails.value!!
            if (mRoomDetaileData.allPicList != null && mRoomDetaileData.allPicList.size > 0) {
                mView.ivTitle.loadImage(mRoomDetaileData.allPicList[0].url)
            }
            mView.titleName.text = mRoomDetaileData.houseName
            mView.money.text = "￥${mRoomDetaileData.rentPrice}"
            mView.money.appendSizeSpan("/月", 0.46f)
                .colorSpan(
                    "",
                    mView.money.text.length - 2..mView.money.text.length,
                    color(R.color.black_66)
                )
                .styleSpan("", 0..mView.money.text.length - 2)
            mView.desc.text =
                "${mRoomDetaileData.doorName}\u0020\u0020${mRoomDetaileData.area}㎡\u0020\u0020${mRoomDetaileData.orientation}"
            mView.addressDesc.text = mRoomDetaileData.address
        }
    }

    @Suppress("DEPRECATION")
    override fun initListener() {
        mView.selTime.setOnClickListener {
            hideKeyboard()
            mTimePicker?.show()
        }
        mView.call.setOnClickListener {
            if (mView.name.isEmpty()) {
                toast("请输入姓名")
                return@setOnClickListener
            }
            if (!mView.phone.checkPhone()) {
                toast("请输入正确的手机号")
                return@setOnClickListener
            }
            if (mView.time.isEmpty()) {
                toast("请选择时间")
                return@setOnClickListener
            }
            if (intent.getStringExtra("type") == "houseDetails") {
                mViewModel.reservation_apply(
                    name = mView.name.getTextZ(),
                    phone = mView.phone.getTextZ(),
                    seeTime = mView.time.getTextZ(),
                    note = mView.message.getTextZ(),
                    roomTypeId = mHouseTypeData.id
                )
            } else {
                mViewModel.reservation_apply(
                    name = mView.name.getTextZ(),
                    phone = mView.phone.getTextZ(),
                    seeTime = mView.time.getTextZ(),
                    note = mView.message.getTextZ(),
                    houseId = mRoomDetaileData.houseId
                )
            }
        }
    }

    override fun startObserve() {
        mViewModel.reservation_apply.observe(this, androidx.lifecycle.Observer {
            getDialog("预约成功", "您已预约成功，管家会尽快与您联系看房") {
                mShareViewModel.houseDetails.value = null
                mShareViewModel.roomDetails.value = null
                finish()
            }.show()
        })
        mViewModel.reservation_apply.state.observe(this, getObserver(mViewModel.reservation_apply))
    }

}